package com.example.demo20250517.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo20250517.domain.Question;
import com.example.demo20250517.domain.QuestionItem;
import com.example.demo20250517.mapper.QuestionItemMapper;
import com.example.demo20250517.mapper.QuestionMapper;
import com.example.demo20250517.service.QuestionItemService;
import com.example.demo20250517.vo.QuestionItemAddVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author banban
 * @description 针对表【question_item】的数据库操作Service实现
 * @createDate 2025-05-17 10:33:17
 */
@Service
public class QuestionItemServiceImpl extends ServiceImpl<QuestionItemMapper, QuestionItem>
        implements QuestionItemService {

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public Boolean add(QuestionItemAddVo questionItemAddVo) {
        QuestionItem questionItem = new QuestionItem();
        BeanUtils.copyProperties(questionItemAddVo, questionItem);
        questionItem.setConfig(JSON.toJSONString(questionItemAddVo.getConfigList()));
        baseMapper.insert(questionItem);
        Question question = questionMapper.selectById(questionItemAddVo.getQuestionId());
        question.setItemNum(baseMapper.selectCount(new QueryWrapper<QuestionItem>().eq("question_id", questionItemAddVo.getQuestionId())));
        questionMapper.updateById(question);
        return true;
    }


}




